﻿using System;
using System.Linq;
using System.Windows.Forms;
using IGM.DataProvider;
using IGM.DataProvider.Entites;
using IGM.Framework.Commons;
using IGM.Presentation.IgmServiceReference;

namespace IGM.Presentation.Materials
{
    public partial class OtherControl : UserControl
    {
        private readonly DomainServiceClient _domainServiceClient;

        public OtherControl()
        {
            InitializeComponent();

            _domainServiceClient = new DomainServiceClient();
        }

        private void OtherControlLoad(object sender, EventArgs e)
        {
            LoadMajors();
            LoadKindInterns();
            LoadKindMarks();
            Dock = DockStyle.Top;
        }

        #region -- Major --

        private void LoadMajors()
        {
            dgvMajors.DataSource = _domainServiceClient.GetMajors().Cast<Major>().ToList();

            DataGridViewHelper.ShowHideColumns(dgvMajors, new[]
                                                                   {
                                                                       (string) typeof (Major).Properties().IsChecked,
                                                                       (string) typeof (Major).Properties().Name,"Code"
                                                                   });
            DataGridViewHelper.ShowIsCheck(dgvMajors);
            DataGridViewHelper.ApplyCheckedAllOrUncheckAll(dgvMajors, false);
            DataGridViewHelper.ChangeDgvHeaderText(dgvMajors, new[] { "IsChecked", "Code", "Name" },
                                                                   new[] { "Chọn", "Kí hiệu của ngành", "Ngành" });

            MajorCurrentRowChanged();
        }

        private void MajorCurrentRowChanged()
        {
            if (dgvMajors.CurrentRow != null)
            {
                txtMajorName.Text = dgvMajors.CurrentRow.Cells["Name"].Value.ToString();
                txtMajorCode.Text = dgvMajors.CurrentRow.Cells["Code"].Value.ToString();
            }

            else
                txtMajorName.Text = txtMajorCode.Text = string.Empty;
        } 

        private void DgvMajorsCurrentCellChanged(object sender, EventArgs e)
        {
            MajorCurrentRowChanged();
        }

        private void DgvMajorsCellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (dgvMajors == null ||
               dgvMajors.Columns[(string)typeof(Major).Properties().IsChecked] == null ||
               dgvMajors.Columns[(string)typeof(Major).Properties().IsChecked].Index != e.ColumnIndex)
                return;

            dgvMajors[e.ColumnIndex, e.RowIndex].Value =
                !(bool)dgvMajors[e.ColumnIndex, e.RowIndex].Value;

            ckbAllMajors.Checked = DataGridViewHelper.AllRowsIsCheck(dgvMajors);
        }

        private void CkbAllMajorsCheckedChanged(object sender, EventArgs e)
        {
            DataGridViewHelper.ApplyCheckedAllOrUncheckAll(dgvMajors, ckbAllMajors.Checked);
        }

        private void ReloadMajors(string majorName)
        {
            LoadMajors();
            DataGridViewHelper.SetDgvCurrentCell(dgvMajors, majorName);
            MajorCurrentRowChanged();
        }

        #endregion    
    
        #region Kind of Intern

        private void LoadKindInterns()
        {
            dgvKindInterns.DataSource = _domainServiceClient.GetKindInterns().Cast<KindIntern>().ToList();

            DataGridViewHelper.ShowHideColumns(dgvKindInterns, new[]
                                                                   {
                                                                       (string) typeof (KindIntern).Properties().IsChecked,
                                                                       (string) typeof (KindIntern).Properties().Name,"Year"
                                                                   });
            DataGridViewHelper.ShowIsCheck(dgvKindInterns);
            DataGridViewHelper.ApplyCheckedAllOrUncheckAll(dgvKindInterns, false);
            DataGridViewHelper.ChangeDgvHeaderText(dgvKindInterns, new[] { "IsChecked", "Name", "Year" },
                                                                   new[] { "Chọn", "Phân loại", "Năm" });
            KindInternCurrentRowChanged();
        }

        private void KindInternCurrentRowChanged()
        {
            if (dgvKindInterns.CurrentRow != null)
            {
                txtKindIntern.Text = dgvKindInterns.CurrentRow.Cells["Name"].Value.ToString();
                txtYear.Text = dgvKindInterns.CurrentRow.Cells["Year"].Value.ToString();
            }

            else
                txtKindIntern.Text = txtYear.Text = string.Empty;
        }

        private void DgvKindInternsCurrentCellChanged(object sender, EventArgs e)
        {
            KindInternCurrentRowChanged();
        }

        private void DgvKindInternsCellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (dgvKindInterns == null ||
               dgvKindInterns.Columns[(string)typeof(KindIntern).Properties().IsChecked] == null ||
               dgvKindInterns.Columns[(string)typeof(KindIntern).Properties().IsChecked].Index != e.ColumnIndex)
                return;

            dgvKindInterns[e.ColumnIndex, e.RowIndex].Value =
                !(bool)dgvKindInterns[e.ColumnIndex, e.RowIndex].Value;

            ckbAllKindInterns.Checked = DataGridViewHelper.AllRowsIsCheck(dgvKindInterns);
        }

        private void CkbAllKindInternsCheckedChanged(object sender, EventArgs e)
        {
            DataGridViewHelper.ApplyCheckedAllOrUncheckAll(dgvKindInterns, ckbAllKindInterns.Checked);
        }

        private void ReloadKindInterns(string kindinternName)
        {
            LoadKindInterns();
            DataGridViewHelper.SetDgvCurrentCell(dgvKindInterns, kindinternName);
            KindInternCurrentRowChanged();
        }
   
        #endregion

        #region Kind of Mark

        private void LoadKindMarks()
        {
            dgvKindMarks.DataSource = _domainServiceClient.GetKindMarks().Cast<KindMark>().ToList(); ;

            DataGridViewHelper.ShowHideColumns(dgvKindMarks, new[]
                                                                   {
                                                                       (string) typeof (KindMark).Properties().IsChecked,
                                                                       (string) typeof (KindMark).Properties().Name
                                                                   });
            DataGridViewHelper.ShowIsCheck(dgvKindMarks);
            DataGridViewHelper.ApplyCheckedAllOrUncheckAll(dgvKindMarks, false);
            DataGridViewHelper.ChangeDgvHeaderText(dgvKindMarks, new[] { "IsChecked", "Name" },
                                                                   new[] { "Chọn","Loại điểm số" });

            KindMarkCurrentRowChanged();
        }

        private void KindMarkCurrentRowChanged()
        {
            txtKindMark.Text = dgvKindMarks.CurrentRow != null ? dgvKindMarks.CurrentRow.Cells["Name"].Value.ToString() : string.Empty;
        }

        private void DgvKindMarksCurrentCellChanged(object sender, EventArgs e)
        {
            KindMarkCurrentRowChanged();
        }

        private void DgvKindMarksCellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (dgvKindMarks == null ||
               dgvKindMarks.Columns[(string)typeof(KindMark).Properties().IsChecked] == null ||
               dgvKindMarks.Columns[(string)typeof(KindMark).Properties().IsChecked].Index != e.ColumnIndex)
                return;

            dgvKindMarks[e.ColumnIndex, e.RowIndex].Value =
                !(bool)dgvKindMarks[e.ColumnIndex, e.RowIndex].Value;

            ckbKindMarks.Checked = DataGridViewHelper.AllRowsIsCheck(dgvKindMarks);
        }

        private void CkbAllKindMarksCheckedChanged(object sender, EventArgs e)
        {
            DataGridViewHelper.ApplyCheckedAllOrUncheckAll(dgvKindMarks, ckbKindMarks.Checked);
        }

        private void ReloadKindMarks(string kindmarkName)
        {
            LoadKindMarks();
            DataGridViewHelper.SetDgvCurrentCell(dgvKindMarks, kindmarkName);
            KindMarkCurrentRowChanged();
        }

        #endregion
    }
}
